﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TouristAgencyDataLayer;
using System.Text;

namespace TouristAgencyAsp
{
    public partial class MakeReservation : System.Web.UI.Page
    {
        private string id;

        protected void Page_Load(object sender, EventArgs e)
        {
            id = Request.Params["tourId"];
            if (id != null)
            {
                LoadTour();
                this.addBtn.Visible = true;
                if (Request.Params["perId"] != null)
                {
                    Session.Add("perId" + Convert.ToInt32(Request.Params["perId"]), Convert.ToInt32(Request.Params["perId"]));
                    this.createBtn.Visible = true;
                }
            }
            else
            {
                toursRepeater.DataSource = Tours.GetTours();
                DataBind();
            }
        }

        private void LoadTour()
        {
            Tour t = Tours.GetTourById(Convert.ToInt32(id));
            this.tourIdLbl.Text = string.Format("Tour id: {0}", t.TourId);
            this.startDateLbl.Text = string.Format("Start Date: {0:dd/MM/yyyy}", t.StartDate);
            this.endDateLbl.Text = string.Format("End Date: {0:dd/MM/yyyy}", t.EndDate);
            this.destinationLbl.Text = string.Format("Destination: {0}", Destinations.GetDestinationById(t.DestinationId).Destination1);
            if (Boards.GetBoardById(t.BoardId) != null)
            {
                this.boardLbl.Text = string.Format("Board: {0}", Boards.GetBoardById(t.BoardId).BoardType);
            }
            else
            {
                this.boardLbl.Text = "Board: ";
            }
            if (MeansOfTransports.GetMeanOfTransportById(t.MeanOfTransportId) != null)
            {

                this.meanLbl.Text = string.Format("Mean Of Transport: {0}", MeansOfTransports.GetMeanOfTransportById(t.MeanOfTransportId).MeanOFTransport);
            }
            else
            {
                this.meanLbl.Text = "Mean Of Transport:";
            }
            StringBuilder sb = new StringBuilder();
            sb.Append("Hotel Facilities: ");
            var hotelFacilities = HotelFacilities.GetFacilitiesForTour(t.TourId);
            foreach (var hf in hotelFacilities)
            {
                sb.Append(string.Format("{0} ", hf.HotelFacility1));
            }
            this.hotelFacLbl.Text = sb.ToString();
            this.priceLbl.Text = string.Format("Price for one person: {0:c}", t.Price);
        }

        protected string FindDestinationName(int id)
        {
            return Destinations.GetDestinationById(id).Destination1;
        }

        protected void addBtn_Click(object sender, EventArgs e)
        {
            Response.Redirect(string.Format("~/AddPerson.aspx?tourId={0}", id));
        }

        protected void createBtn_Click(object sender, EventArgs e)
        {
            List<int> personsIds = new List<int>();
            for (int i = 0; i < Session.Count; i++)
            {
                personsIds.Add((int)Session[i]);
            }
            int tourId = Convert.ToInt32(id);
            Tour t = Tours.GetTourById(tourId);
            int resId = Reservations.CreateReservation(tourId, 1, 0);
            foreach (var perId in personsIds)
            {
                ReservationsPersons.CreateReservationPerson(resId, perId);
                Reservations.AddToPrice(resId, t.Price);
            }

            Response.Redirect("~/ReservationCreated.aspx?resId=" + resId);
        }
    }
}